﻿using HookupAppForm.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace HookupAppForm
{
    //声明委托 和 事件
    public delegate void TransfDelegate(string tenantId, string token, string tenancyName, string userName);
    public partial class FormLogin : Form
    {
        string webAPI = ConfigurationManager.AppSettings["WebAPI"];
        public FormLogin()
        {
            InitializeComponent();
        }
        public event TransfDelegate TransfEvent;
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            //userNameOrEmailAddressBox.Text = "admin";
            //passwordBox.Text = "123qwe";

            APIClient client = new APIClient(webAPI);
            if (string.IsNullOrEmpty(userNameOrEmailAddressBox.Text))
            {
                Utils.SysMsg("用户名不能为空");
            }
            else if (string.IsNullOrEmpty(passwordBox.Text))
            {
                Utils.SysMsg("密码不能为空");
            }
            else
            {
                AccountDto obj = new AccountDto();
                obj.tenancyName = DefaulttextBox.Text.Trim();
                string jsons = JsonConvert.SerializeObject(obj);
                string Accurl = "api/services/app/Account/IsTenantAvailable";                
                string result = client.Post(Accurl,jsons);
                if (Utils.ExceptionHandler(result) == null)
                {
                    SuccessAccount account = JsonConvert.DeserializeObject<SuccessAccount>(result);
                    string tenantId = account.result.tenantId.ToString();
                    string url = "api/TokenAuth/Authenticate";
                    client.TenantId = tenantId;
                    LoginDto dto = new LoginDto();
                    dto.userNameOrEmailAddress = userNameOrEmailAddressBox.Text.Trim();
                    dto.password = passwordBox.Text.Trim();
                    jsons = JsonConvert.SerializeObject(dto);
                    result = client.Post(url,jsons);
                    if (Utils.ExceptionHandler(result) == null)
                    {
                        LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(result);
                        string token = loginResult.result.accessToken;
                        //触发事件
                        TransfEvent(tenantId, "Bearer " + token, DefaulttextBox.Text, userNameOrEmailAddressBox.Text);
                        Utils.SysMsg("登录成功！");
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
            }
        }
        private void FormLogin_Load(object sender, EventArgs e)
        {
            DefaulttextBox.Text = "Default";//设置默认值
            pictureBox1.BackColor = Color.Transparent;
        }

        private void buttonCfg_Click(object sender, EventArgs e)
        {
            FormConfig formConfig = new FormConfig();
            formConfig.ShowDialog();
            formConfig.Owner = this;
        }
    }
}
